from get_chrome_jigou import get_chrombrowser
from parsel import Selector
import time
import csv
import pandas as pd
import random


def parse_yiyuan(yiyuan,chrome):
    url = r'https://banquan.tianyancha.com/rj/search/%s' % yiyuan
    chrome.get(url)
    soup = Selector(chrome.page_source)
    num = soup.xpath('//span[@class="tips-num"]/text()').extract_first()
    data = [yiyuan, num]
    print(data)
    with open('天眼查_医院软著信息.csv', 'a+', encoding='utf-8', newline='') as file:
        csv_writer = csv.writer(file)
        csv_writer.writerow(data)
    

if __name__ == '__main__':
    df = pd.read_excel('医院_列表.xlsx')
    yiyuan_list = list(df['医院名'])
    chrome = get_chrombrowser()
    for yiyuan in yiyuan_list:
        parse_yiyuan(yiyuan,chrome)
        time.sleep(random.randint(1,3))
    chrome.quit()
